<?php
/** This script accepts the $_POST fields of index.php and writes the matrix to an Excel 2007 format.
 *	Using PHPExcel (go to the @see tag) to generate the Excel file.
 *
 *  @author		Jos Nienhuis
 *  @since		15-08-2013
 *  @version	1.0
 * 	@copyright 	Copyright (c) 2013 Jos Nienhuis
 * 	@see		http://phpexcel.codeplex.com/
 */

if($_SERVER['REQUEST_METHOD'] != 'POST'){
	die(header('location:index.php'));
}

// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="afstanden_matrix.xlsx"');
header('Cache-Control: max-age=0');

require_once('PHPExcel.php');
$objPHPExcel = new PHPExcel();
$activeSheet = $objPHPExcel->getActiveSheet();

foreach ($_POST['horizontal'] as $columnIndex => $column) {
	$activeSheet->setCellValueByColumnAndRow($columnIndex, 1, $column);
}

foreach ($_POST['vertical'] as $rowIndex => $row) {
	$activeSheet->setCellValueByColumnAndRow(0, $rowIndex + 1, $row);
}

foreach ($_POST['row'] as $rowIndex => $row) {
	foreach($row as $columnIndex => $column){
		$activeSheet->setCellValueByColumnAndRow($columnIndex, $rowIndex + 1, $column);
	}
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

?>